package jgl.vajra.driver.manager;

import android.Manifest;
import android.content.Context;
import android.text.TextUtils;
import android.util.Log;

import jgl.vajra.driver.R;
import jgl.vajra.driver.part.splash.SplashActivity;
import jgl.vajra.driver.widget.manager.SPManager;
import jgl.vajra.driver.widget.permissions.PermissionListener;
import jgl.vajra.driver.widget.permissions.PermissionsUtil;
import jgl.vajra.driver.widget.push.JPushManager;
import jgl.vajra.driver.widget.utils.CommonUtils;
import jgl.vajra.driver.widget.utils.UIUtils;
import com.orhanobut.logger.Logger;

import androidx.fragment.app.FragmentActivity;
import cn.jpush.android.api.JPushInterface;

public class SDKManager {
    private static class SDKManagerSingleton {
        private static final SDKManager INSTANCE = new SDKManager();

    }

    public static SDKManager getInstance() {
        return SDKManager.SDKManagerSingleton.INSTANCE;
    }

    private SDKManager() {
    }

    public void initSDK(Context context) {
        if (SPManager.AgreementSplashData.getAgreementStatus()) {
            initJpush(context);
            initJPushHandler();
        }
    }

    private void initJpush(Context context) {

        JPushInterface.setDebugMode(!CommonUtils.isAppRelease());
        JPushInterface.init(context);

        Log.e("aaaa", "initJpush: " + JPushInterface.getRegistrationID(context));
    }

    private void initJPushHandler() {
        // 设置打开应用首个Activity
        JPushManager.getInstance().setLauncherClass(SplashActivity.class);
        // 设置推送处理接口
        JPushManager.getInstance().setJPushHandlerListener(new JPushManager.JPushHandlerListener() {
            @Override
            public boolean isHandlerPush(String value) {
                // 防止为null
                if (TextUtils.isEmpty(value)) {
                    return false;
                }
                // ---
                if (value.equals(SplashActivity.class.getSimpleName())) {
                    return false; // 属于欢迎页面则不进行处理
                }
                Logger.d("isHandlerPush-----" + value);
                // 默认进行处理
                return true;
            }

            @Override
            public void onPushHandler(FragmentActivity activity, String pushMsgId, String romType, String nTitle, String nContent, String nExtras) {
                StringBuffer buffer = new StringBuffer();
                buffer.append("\npushMsgId: " + pushMsgId);
                buffer.append("\nromType: " + romType);
                buffer.append("\nnTitle: " + nTitle);
                buffer.append("\nnContent: " + nContent);
                buffer.append("\nnExtras: " + nExtras);
                Logger.e("JPushManager" + buffer);
            }


        });
    }


    public void requestStoragePermission(Context context,boolean isExitApp,int type, PermissionListener listener) {
        String[] permissions = {
                Manifest.permission.WRITE_EXTERNAL_STORAGE,

        };
        PermissionsUtil.TipInfo info = new PermissionsUtil.TipInfo
                (UIUtils.getString(R.string.hint),
                        "视频下载需要使用到读写存储卡的权限,请同意此权限,否则将不能使用此功能",
                       "不同意",
                       "同意");

        PermissionsUtil.requestPermission(context, listener, permissions, true, info);

    }
}
